﻿<script type="text/javascript">
    function openThis(val) {
        var id = '#emailSubject_' + val;
        var div = $(id);
        div.css('display', 'block');
    }

    function closeSubject(val) {
        var div = $('#emailSubject_' + val);
        div.css('display', 'none');

        // Update collection on server set Read = true
        $.ajax({
            type: 'POST',
            url: '../home/markEmailRead',
            data: { Id: val }
        });

        // Update UI set Read = true
        handleCheckboxes(val, 'True');
    }

    function handleCheckboxes(val, isChecked) {
        var isRead = false;
        if (isChecked != 'False')
            isRead = true;

        var chkBox = $('#chkBox_' + val + '_read');
        var elemName = 'chkBox_' + val + '_read';
        $('input[name=' + elemName + ']').attr('checked', isRead);
    }
</script>
@{
    ViewBag.Title = "Inbox";
}

<h2>Inbox</h2>
@*
    Subject
    Date
    Read
*@

<table style="width:100%;">
    <tr>
        <th style="width:60%; text-align:left; vertical-align:top;">
            Subject
        </th>
        <th style="width:25%; text-align:left; vertical-align:top;">
            Date
        </th>
        <th style="width:15%; text-align:left; vertical-align:top;">
            Read
        </th>
    </tr>
    @{
        List<CGE.Controllers.Email> emails = ViewBag.Emails as List<CGE.Controllers.Email>;
    }
    @foreach (CGE.Controllers.Email email in emails)
    {
        <tr>
            <td>
                <a href="#" onclick="openThis('@email.Id.ToString()'); return false;">@email.Subject</a>
            </td>
            <td>
                @email.Date
            </td>
            <td>
                <input type="checkbox"  id="chkBox_@(email.Id)_read" name="chkBox_@(email.Id)_read" disabled="disabled" />
                <script type="text/javascript">
                    handleCheckboxes('@(email.Id)', '@(email.Read)');
                </script>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <a href="../Home/DeleteEmail/@(email.Id)">@Html.Image("Logo", "~/Assets/images/garbageCan.png", "CGE Logo", new { @style=" width:20px;" })</a>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <div id="emailSubject_@(email.Id)" style="display:none;">
                    <a href="#" style="float:right;" onclick="closeSubject('@email.Id'); return false;">Close</a>
                    <br /><br />
                    @email.Content
                </div>
            </td>
        </tr>
    }
    
</table>

